import {Column, Entity, JoinColumn, JoinTable, ManyToMany, PrimaryGeneratedColumn} from "typeorm";
import {ApiProperty} from "@nestjs/swagger";
import {BaseEntity} from "./BaseEntity";
import {DefaultEnum} from "../common/enum/DefaultEnum";
import {SysUser} from "./SysUser";
import {SysMenu} from "./SysMenu";
import {RoleEnum} from "../common/enum/RoleEnum";
import {IsNotEmpty} from "class-validator";
import {Validate_Mods} from "../common/enum/ValidateModsEnum";

@Entity("sys_role")
export class SysRole extends BaseEntity{


    constructor(createBy?: number, createTime?: Date, updateBy?: number, updateTime?: Date, roleId?: number, roleName?: string,
                level?: number, isDefault?: number, type?: number, users?: SysUser[], menus?: SysMenu[]) {
        super(createBy, createTime, updateBy, updateTime);
        this.roleId = roleId;
        this.roleName = roleName;
        this.level = level;
        this.isDefault = isDefault;
        this.type = type;
        this.users = users;
        this.menus = menus;
    }

    @ApiProperty({description: "角色主键ID"})
    @IsNotEmpty({message: "id不能为空!", groups: [Validate_Mods.delete]})
    @PrimaryGeneratedColumn({name: "role_id", comment: "角色主键ID"})
    roleId: number;

    @ApiProperty({description: "角色名称"})
    @Column({name: "role_name", comment: "角色名称", length: 20})
    roleName: string;

    @ApiProperty({ description: "角色等级：1-9(1是最高级别)"})
    @Column({comment: "角色等级：1-9(9是最高级别)", default: 9})
    level: number;

    @ApiProperty({description: '是否默认（默认标识不可删除，仅管理员可操作）：0-否；1-是', example: 0})
    @Column({name:'is_default', comment: '是否默认（默认标识不可删除，仅管理员可操作）：0-否；1-是', default: 0})
    /** @see DefaultEnum */
    isDefault: number;

    @ApiProperty({description: "角色类型"})
    /** @see RoleEnum */
    @Column({comment: "角色类型: 1-管理员；2-系统人员；3-店长；4-店员"})
    type: number;


    /**
     * 关联关系
     */
    @ManyToMany(type => SysUser, sysUser => sysUser.roles, {createForeignKeyConstraints: false})
    users: SysUser[];

    @ManyToMany(type => SysMenu, menu => menu.roles, {createForeignKeyConstraints: false})
    @JoinTable({name: "sys_role_menu", joinColumn: {name: "role_id"}, inverseJoinColumn: {name: "menu_id"} })
    menus: SysMenu[]


}
